package com.itheima.testservlet;

import com.itheima.service.UserService;
import com.itheima.service.impl.UserServiceImp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

public class BaseServlet extends HttpServlet {
    public UserService userService;

    public void init() throws ServletException {
        userService=new UserServiceImp();
    }
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("Text/Html;charset=UTF-8");
        String uri = req.getRequestURI();
        //获取URI
        String me = uri.substring(uri.lastIndexOf("/") + 1);
        //执行userservlet中的方法
        try {
            Method method = this.getClass().getDeclaredMethod(me, HttpServletRequest.class, HttpServletResponse.class);
            method.setAccessible(true);
            method.invoke(this,req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
